﻿using AntdUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Controls
{
    public enum LedStatus
    {
        Default,    // 默认状态(灰色)
        Running,    // 运行状态(绿色)
        Idle,       // 闲置状态(橙色)
        Alarm       // 报警状态(红色)
    }


    [ToolboxItem(true)]
    [Description("一个用于显示通信状态的LED指示器控件")]
    public partial class UCLedAvatar : UserControl
    {
        private Avatar avatar;
        private LedStatus _status = LedStatus.Default;
        private bool _isBlinking = false;
        private bool _blinkState = false;
        private System.Windows.Forms.Timer _blinkTimer;

        // 定义状态对应的颜色
        private static readonly Dictionary<LedStatus, Color> StatusColors = new Dictionary<LedStatus, Color>
    {
        { LedStatus.Default, Color.FromArgb(128, 128, 128) },    // 灰色
        { LedStatus.Running, Color.FromArgb(16, 192, 101) },       // 绿色 #10c065
        { LedStatus.Idle, Color.FromArgb(255, 147, 0) },        // 橙色 #ff9300
        { LedStatus.Alarm, Color.FromArgb(241, 11, 18) }         // 红色 #f10b12
    };

        // SVG 模板
        private const string SVG_TEMPLATE = @"<svg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg'>
        <path fill='{0}' d='M187.7863218 595.62837703c0-179.04700379 145.16667442-324.2136782 324.2136782-324.21367818s324.2136782 145.16667442 324.2136782 324.21367818v405.26709776H187.7863218V595.62837703z M133.75070835 595.62837703c0-208.90167956 169.34761084-378.24929165 378.24929165-378.24929164s378.24929165 169.34761084 378.24929165 378.24929164v405.26709776a54.03561344 54.03561344 0 0 1-54.03561345 54.0356122H187.7863218a54.03561344 54.03561344 0 0 1-54.03561345-54.0356122V595.62837703z m378.24929165-270.17806598c-149.21934537 0-270.17806475 120.95871935-270.17806475 270.17806598v351.23148431h540.3561295V595.62837703c0-149.21934537-120.95871935-270.17806475-270.17806475-270.17806598z M586.13686148 496.04074181L526.37347263 595.62837703h162.10683911l-157.94609605 263.26150672-92.67107717-55.60264581L497.62652737 703.69960271h-162.10683911l157.94609605-263.2615067 92.67107717 55.6026458z M-1.33832339 946.85986134h1026.67664678v108.07122565H-1.33832339v-108.07122565z M566.03561344-25.78117324v162.10683911h-108.07122688V-25.78117324h108.07122688z M966.65564763 190.3612793l-114.60953522 114.63655263-76.43337465-76.4333746L890.24929165 113.95492206 966.65564763 190.3612793z M133.75070835 113.95492206l114.63655389 114.60953527-76.43337465 76.4333746L57.34435237 190.3612793 133.75070835 113.95492206z'/>
    </svg>";

        public UCLedAvatar()
        {
            InitializeComponent();
            InitializeAvatar();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;
        }


        private void InitializeAvatar()
        {
            avatar = new Avatar
            {
                Dock = DockStyle.Fill,
                Round = false,
                ImageSvg = string.Format(SVG_TEMPLATE, ColorTranslator.ToHtml(StatusColors[LedStatus.Default]))
            };

            this.Controls.Add(avatar);
            InitializeTimer();
        }

        private void InitializeTimer()
        {
            _blinkTimer = new System.Windows.Forms.Timer();
            _blinkTimer.Tick += BlinkTimer_Tick;
            _blinkTimer.Interval = 500; // 默认闪烁间隔
        }

        [Category("行为")]
        [DefaultValue(LedStatus.Default)]
        public LedStatus Status
        {
            get => _status;
            set
            {
                if (_status != value)
                {
                    _status = value;
                    UpdateLedState();
                }
            }
        }

        [Category("行为")]
        [DefaultValue(false)]
        public bool IsBlinking
        {
            get => _isBlinking;
            set
            {
                if (_isBlinking != value)
                {
                    _isBlinking = value;
                    if (_isBlinking)
                    {
                        _blinkTimer.Start();
                    }
                    else
                    {
                        _blinkTimer.Stop();
                        _blinkState = false;
                        UpdateLedState();
                    }
                }
            }
        }

        [Category("行为")]
        [DefaultValue(500)]
        public int BlinkInterval
        {
            get => _blinkTimer.Interval;
            set
            {
                if (value > 0 && _blinkTimer.Interval != value)
                {
                    _blinkTimer.Interval = value;
                }
            }
        }

        private void BlinkTimer_Tick(object sender, EventArgs e)
        {
            _blinkState = !_blinkState;
            UpdateLedState();
        }

        private void UpdateLedState()
        {
            Color currentColor;
            if (_status == LedStatus.Alarm && _isBlinking)
            {
                // 报警状态下的闪烁在灰色和红色之间切换
                currentColor = _blinkState ? StatusColors[LedStatus.Alarm] : StatusColors[LedStatus.Default];
            }
            else
            {
                // 非报警状态不闪烁
                currentColor = StatusColors[_status];
            }

            avatar.ImageSvg = string.Format(SVG_TEMPLATE, ColorTranslator.ToHtml(currentColor));
        }

    }
}
